package com.ys.controller.web.sys;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;

import com.bo.enume.sys.StatusEnum;
import com.ys.config.MyConfig;
import com.ys.entity.shiro.LoginUser;
import com.ys.entity.sys.SysMenu;
import com.ys.enume.sys.SysMenuEnum;
import com.ys.service.sys.CommonService;
import com.ys.util.shiro.ShiroUtils;

import lombok.AllArgsConstructor;
import lombok.Data;

@CrossOrigin
@Controller
public class WebController {
	
	@Autowired
	private CommonService commonService;
	
	
	/**
	 * 登录登出页
	 */
	@GetMapping({"/login", "/logout"})
	public String login(Model model){
		
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			subject.logout();
		}
		return "login";
	}
	
	
	/**
	 * 示例页
	 */
	@GetMapping("/demo")
	public String demo(Model model){
		return "demo";
	}
	
	/**
	 * 首页
	 */
	@GetMapping({ "", "/", "/index" })
	public String index(Model model){
		
		LoginUser loginUser = ShiroUtils.getLoginUser();
		if (loginUser == null) {
			
			Subject subject = SecurityUtils.getSubject();
			if (subject != null) {
				subject.logout();
			}
			return "login";
		}
		Long roleId = loginUser.getRoleId();
		
		// 顶部菜单数组
		List<SysMenu> listMenuOne = new SysMenu().setStatus(StatusEnum.ON_USED.value)
				.setLev(SysMenuEnum.Lev.ONE.value)
				.setOrderby("A.SortNo asc")
				.queryList();
		List<SysMenu> listMenuTwo = new SysMenu().setStatus(StatusEnum.ON_USED.value)
				.setLev(SysMenuEnum.Lev.TWO.value)
				.setOrderby("A.SortNo asc")
				.queryList();
		List<SysMenu> listMenuThree = new SysMenu().setStatus(StatusEnum.ON_USED.value)
				.setLev(SysMenuEnum.Lev.THREE.value)
				.setOrderby("A.SortNo asc")
				.where(roleId.longValue() != 1L ? "A.Id in (select MenuId from YS_RoleMenu where RoleId = '" + roleId + "')" : "1=1")
				.queryList();

		// 一级菜单数组
		List<IndexDto.MenuDto> menuOneArr = new ArrayList<IndexDto.MenuDto>();
		for(SysMenu ysMenu1 : listMenuOne) {
			List<IndexDto.MenuDto.SubDto> menuTwoArr = new ArrayList<IndexDto.MenuDto.SubDto>();
			
			// 添加二级菜单
			for(SysMenu ysMenu2 : listMenuTwo) {
				if (ysMenu2.getPid().longValue() != ysMenu1.getId().longValue()) {
					continue;
				} else {
					// 添加三级菜单
					List<IndexDto.MenuDto.SubDto.ModuleDto> menuThreeArr = new ArrayList<IndexDto.MenuDto.SubDto.ModuleDto>();
					for(SysMenu ysMenu3 : listMenuThree) {
						if (ysMenu3.getPid().longValue() != ysMenu2.getId().longValue()) {
							continue;
						}
						String name = ysMenu3.getName();
						String linkTo = "/indexCommonOfMenu?menuId=" + ysMenu3.getId();
						menuThreeArr.add(new IndexDto.MenuDto.SubDto.ModuleDto(name, linkTo));
					}
					if (menuThreeArr.size() == 0) {
						continue;
					}
					String name = ysMenu2.getName();
					menuTwoArr.add(new IndexDto.MenuDto.SubDto(name, menuThreeArr));
				}
			}
			
			if (menuTwoArr.size() == 0) {
				continue;
			}
			String name = ysMenu1.getName();
			menuOneArr.add(new IndexDto.MenuDto(name, menuTwoArr));
		}
		
		String basePath = MyConfig.getUrlDomain();
		IndexDto.UserInfoDto userInfo = new IndexDto.UserInfoDto(loginUser.getAdminId(), loginUser.getAdminName(), basePath);
		
		IndexDto dto = new IndexDto(menuOneArr, userInfo);
		model.addAttribute("dto", dto);
		
		return "sys/index";
	}
	
	@Data
	@AllArgsConstructor
	public static class IndexDto {
		private List<MenuDto> menuLeftArr; // 左边菜单数组
		private UserInfoDto userInfo;

		@Data
		@AllArgsConstructor
		public static class MenuDto {
			private String name; // 菜单名称
			private List<SubDto> subArr; // 二级菜单数组

			@Data
			@AllArgsConstructor
			public static class SubDto {
				private String name; // 菜单名称
				private List<ModuleDto> moduleArr; // 模块数组

				@Data
				@AllArgsConstructor
				public static class ModuleDto {
					private String name; // 菜单名称
					private String linkTo; // 跳转链接
				}
			}
		}

		@Data
		@AllArgsConstructor
		public static class OrgDto {
			private String name; // 部门名称
			private List<ItemDto> itemArr; // 部门数组

			@Data
			@AllArgsConstructor
			public static class ItemDto {
				private String name; // 部门名称
				private String orgId; // 部门ID
			}
		}

		@Data
		@AllArgsConstructor
		public static class OperateCompanyDto {
			private String name; // 运营公司名称
			private List<ItemDto> itemArr; // 运营公司数组

			@Data
			@AllArgsConstructor
			public static class ItemDto {
				private String name; // 运营公司名称
				private Long id; // 运营公司ID
			}
		}

		@Data
		@AllArgsConstructor
		public static class UserInfoDto {
			private Long adminId; // 用户ID
			private String adminName; // 用户名称
			private String basePath;
		}
	}

	/*
	 * 仪表盘
	 */
	@RequiresPermissions("sys:dashboard")
	@GetMapping("/dashboard")
	public String dashboard(Model model) {

		List<String> oneArr = new ArrayList<String>();
		{
			String str = "XX总数：--";
			oneArr.add(str);
		}
		{
			String str = "XX总数：--";
			oneArr.add(str);
		}
		
		DashboardIndexDto dto = new DashboardIndexDto(oneArr);
    	
		model.addAttribute("dto", dto);
		return "dashboard";
	}
	
	@Data
	@AllArgsConstructor
	public static class DashboardIndexDto {
		private List<String> oneArr; // 汇总
	}

	/*
	 * 通用菜单首页
	 */
	@GetMapping("/indexCommonOfMenu")
	public String indexCommonOfMenu(Model model, HttpServletRequest request, Long menuId) {

		return commonService.indexCommonOfMenu(model, request, menuId);
	}

	/*
	 * 导入页面
	 */
	@GetMapping("/importCommon")
	public String importCommon(Model model, HttpServletRequest request,
			Long menuId) {
		
		ImportCommonDto dto = new ImportCommonDto(menuId);
		model.addAttribute("dto", dto);
		return "sys/common/import_common";
	}
	@Data
	@AllArgsConstructor
	public class ImportCommonDto {
		private Long menuId;
	}
}
